﻿using System;
using System.Collections.Generic;
using System.Text;
using TK3DSim.Action;
using TK3DSim;
using TK3DSim.Mil;
using TK3DSim.Other;
using TerraExplorerX;
using System.Windows.Forms;

namespace Tpro6Container.Class.ObjectClass
{
    public class CreatAllArmy
    {
        public CreatAllArmy()
        {
            creat();
        }
        void creat()
        {
            Armys allArmy = Global.Instance.GlobalArmys;
            int armyscun = allArmy.Count;
            int id = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队");
            for (int i = 0; i < armyscun; i++)
            {
                Army one = allArmy[i];
                int ar =  GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\"+one.ArmyName);
                if(ar>0)
                {
                    GlobalObjects.Instance.World3D.ProjectTree.DeleteItem(ar);
                }
                int armyid = GlobalObjects.Instance.World3D.ProjectTree.CreateGroup(one.ArmyName, id);
                
                IPosition6 armypoint = GlobalObjects.Instance.World3D.Creator.CreatePosition(one.ArmyPost.X, one.ArmyPost.Y, 0,
                    TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, 0, 0, 0, 0);
                  
                if (one.ArmamentCount > 0)
                {
                    int fireid = GlobalObjects.Instance.World3D.ProjectTree.CreateGroup("开火", armyid);
                    int tagetid = GlobalObjects.Instance.World3D.ProjectTree.CreateGroup("目标", armyid);
                    int labelid = GlobalObjects.Instance.World3D.ProjectTree.CreateGroup("标签", armyid);
                    int modelid = GlobalObjects.Instance.World3D.ProjectTree.CreateGroup("模型", armyid);
                    List<IPosition6> points = new List<IPosition6>();
                    int allArmamentCount = one.ArmamentCount + one.ArmamentCount1 + one.ArmamentCount2 + one.ArmamentCount3 + 
                        one.ArmamentCount4 + one.ArmamentCount5 + one.ArmamentCount6 + one.ArmamentCount7 + one.ArmamentCount8 + one.ArmamentCount9;
                    TerraExplorerX.ITerrainImageLabel6 img = GlobalObjects.Instance.World3D.Creator.CreateImageLabel(armypoint, Application.StartupPath + @"\JunBiao" + one.JunBiaoFile, null, armyid, "部队标号");
                    img.Style.Scale = one.JunBiaoFileScal;
                    img.Style.PivotAlignment =  "Center,Center";
                    if (one.IsShuLi)
                    {
                        img.Style.LockMode = LabelLockMode.LM_DECAL;
                    }
                    else
                    {
                        img.Style.LockMode = LabelLockMode.LM_AXIS;
                    }
                    img.Style.MinViewingHeight = one.VisibleHeight;
                    int rownum = 0;
                    if (allArmamentCount % 3 > 0)
                    {
                        rownum = allArmamentCount / 3 + 1;
                    }
                    else
                    {
                        rownum = allArmamentCount / 3;
                    }
                    for (int j = 0; j < rownum; j++)
                    {
                        double X = armypoint.X;
                        double Y = armypoint.Y;

                        if (3 * j < allArmamentCount)
                        {

                             ICoord2D aa=  GlobalObjects.Instance.World3D.CoordServices.MoveCoord(X,  Y, 0, -j*50);
                             points.Add(GlobalObjects.Instance.World3D.Creator.CreatePosition(aa.X, aa.Y, 0,
                    TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, 0, 0, 0, 0));
                        }

                        if (3 * j +1 < allArmamentCount)
                        {
                            ICoord2D aa1 = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(X, Y, 50, -j * 50);
                            points.Add(GlobalObjects.Instance.World3D.Creator.CreatePosition(aa1.X, aa1.Y, 0,
                   TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, 0, 0, 0, 0));
                        }

                        if (3 * j +2 < allArmamentCount)
                        {
                            ICoord2D aa2 = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(X, Y, 100, -j * 50);

                            points.Add(GlobalObjects.Instance.World3D.Creator.CreatePosition(aa2.X, aa2.Y, 0,
                   TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, 0, 0, 0, 0));
                        }

                    }
                    for (int jj = 0; jj < points.Count; jj++)
                    {
                        string equid = "";
                        if (jj < one.ArmamentCount)
                        {
                            equid = one.EquipmentID;
                        }
                        else if (jj < one.ArmamentCount + one.ArmamentCount1)
                        {
                            equid = one.EquipmentID1;
                        }
                        else if (jj < one.ArmamentCount + one.ArmamentCount1 + one.ArmamentCount2)
                        {
                            equid = one.EquipmentID2;
                        }
                        else if (jj < one.ArmamentCount + one.ArmamentCount1 + one.ArmamentCount2 + one.ArmamentCount3)
                        {
                            equid = one.EquipmentID3;
                        }
                        else if (jj < one.ArmamentCount + one.ArmamentCount1 + one.ArmamentCount2 + one.ArmamentCount3 + one.ArmamentCount4)
                        {
                            equid = one.EquipmentID4;
                        }
                        else if (jj < one.ArmamentCount + one.ArmamentCount1 + one.ArmamentCount2 + one.ArmamentCount3 + one.ArmamentCount5 + one.ArmamentCount4)
                        {
                            equid = one.EquipmentID5;
                        }
                        else if (jj < one.ArmamentCount + one.ArmamentCount1 + one.ArmamentCount2 + one.ArmamentCount3 + one.ArmamentCount6 + one.ArmamentCount5 + one.ArmamentCount4)
                        {
                            equid = one.EquipmentID6;
                        }
                        else if (jj < one.ArmamentCount + one.ArmamentCount1 + one.ArmamentCount2 + one.ArmamentCount7 + one.ArmamentCount3 + one.ArmamentCount6 + one.ArmamentCount5 + one.ArmamentCount4)
                        {
                            equid = one.EquipmentID7;
                        }
                        else if (jj < one.ArmamentCount + one.ArmamentCount1 + one.ArmamentCount2 + one.ArmamentCount3 + one.ArmamentCount7 + one.ArmamentCount8 + one.ArmamentCount6 + one.ArmamentCount5 + one.ArmamentCount4)
                        {
                            equid = one.EquipmentID8;
                        }
                        else if (jj < points.Count)
                        {
                            equid = one.EquipmentID9;
                        }

                        if (equid != "")
                        {
                            Armament equipmen = Global.Instance.GlobalArmaments[equid];
                            ITerrainModel6 itmod = GlobalObjects.Instance.World3D.Creator.CreateModel(points[jj], Application.StartupPath + @"\Model\" + equipmen.ModelFileName+"pl2",
                                equipmen.ModelZoom, TerraExplorerX.ModelTypeCode.MT_NORMAL, modelid, equipmen.Name + (jj + 1).ToString());
                            if (jj == 0)
                            {
                                img.Attachment.AttachTo(itmod.ID, 0, 0, 0);
                            }
                            ILabelStyle6 textla = GlobalObjects.Instance.World3D.Creator.CreateLabelStyle(SGLabelStyle.LS_DEFAULT);
                            textla.MaxViewingHeight = one.VisibleHeight;
                            ITerrainLabel6 itl = GlobalObjects.Instance.World3D.Creator.CreateLabel(points[jj], one.ArmyName + (jj + 1).ToString(), "", textla, labelid, "标签");
                            itl.Attachment.AttachTo(itmod.ID, 0, 0, 10);

                            ITerrainImageLabel6 imgfire = GlobalObjects.Instance.World3D.Creator.CreateImageLabel(points[jj], Application.StartupPath + @"\Model\" + equipmen.ShowName, null, fireid, "");
                            ITerrainImageLabel6 imgtaget1 = null;
                            //img.Position. 
                            imgfire.Style.Scale = equipmen.ShowScal;
                            if (equipmen.TargteShowName != "线")
                            {
                                imgtaget1 = GlobalObjects.Instance.World3D.Creator.CreateImageLabel(points[jj], Application.StartupPath + @"\Model\" + equipmen.TargteShowName, null, tagetid, "");
                                imgtaget1.Style.Scale = equipmen.TargteShowScal;
                                GlobalObjects.Instance.World3D.ProjectTree.SetVisibility(imgtaget1.TreeItem.ItemID, false);
                            }
                            else
                            {
                               // GlobalObjects.Instance.World3D.Creator.CreatePolyline(null,255, AltitudeTypeCode.ATC_ON_TERRAIN,tagetid,"联系线");

                            }
                            imgfire.Attachment.AttachTo(itmod.ID, equipmen.FileToModelX, equipmen.FileToModelY, equipmen.FileToModelZ);
                            GlobalObjects.Instance.World3D.ProjectTree.SetVisibility(imgfire.TreeItem.ItemID, false);
                        }

                    }
                }
                else
                {

                    int allArmamentCount = one.ArmamentCount + one.ArmamentCount1 + one.ArmamentCount2 + one.ArmamentCount3 +
                        one.ArmamentCount4 + one.ArmamentCount5 + one.ArmamentCount6 + one.ArmamentCount7 + one.ArmamentCount8 + one.ArmamentCount9;
                    TerraExplorerX.ITerrainImageLabel6 img = GlobalObjects.Instance.World3D.Creator.CreateImageLabel(armypoint, Application.StartupPath + @"\JunBiao" + one.JunBiaoFile, null, armyid, "部队标号");
                    img.Style.Scale = one.JunBiaoFileScal;
                    if (one.IsShuLi)
                    {
                        img.Style.LockMode = LabelLockMode.LM_DECAL;
                    }
                    else
                    {
                        img.Style.LockMode = LabelLockMode.LM_AXIS;
                    }
                    img.Style.MinViewingHeight = one.ModelVisibleHeight;

                    ILabelStyle6 textla = GlobalObjects.Instance.World3D.Creator.CreateLabelStyle(SGLabelStyle.LS_DEFAULT);
                    textla.MinViewingHeight = one.VisibleHeight;
                    GlobalObjects.Instance.World3D.Creator.CreateLabel(armypoint, one.ArmyName, "", textla, armyid, "部队标签");

                }
            }
        }
    }
}
